package cn.iocoder.yudao.module.wms.service.sale;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.wms.dal.dataobject.commodity.CommodityDO;
import cn.iocoder.yudao.module.wms.service.commodity.CommodityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.wms.controller.admin.sale.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.sale.SaleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wms.dal.mysql.sale.SaleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;

/**
 * 销售 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SaleServiceImpl extends ServiceImpl<SaleMapper, SaleDO> implements SaleService {

    @Resource
    private SaleMapper saleMapper;
    @Resource
    private CommodityService commodityService;

    @Override
    public String createSale(SaleSaveReqVO reqVO) {
        // 获取商品价格
        CommodityDO commodityDO = commodityService.getCommodity(reqVO.getCid());
        if (commodityDO == null) {
            throw exception(COMMODITY_NOT_EXISTS);
        }
        // 组装SaleDO，并插入
        SaleDO saleDO = BeanUtils.toBean(reqVO, SaleDO.class);
        String now = LocalDateTimeUtil.formatNormal(LocalDateTime.now());
        saleDO.setId(UUID.randomUUID().toString())
                .setCommodity(commodityDO.getName())
                .setPay(false)
                // TODO 这边是用reqVO.getCount()还是commodityDO.getCount()，应该是前者，但是两者有没有关联限制呢？
                .setPrice(reqVO.getCount() * commodityDO.getPrice())
                .setCreateAt(now)
                .setUpdateAt(now);
        saleMapper.insert(saleDO);
        // 返回
        return saleDO.getId();
    }

    @Override
    public void updateSale(SaleSaveReqVO updateReqVO) {
        // 校验存在
        validateSaleExists(updateReqVO.getId());
        // 更新
        SaleDO updateObj = BeanUtils.toBean(updateReqVO, SaleDO.class);
        updateObj.setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
        saleMapper.updateById(updateObj);
    }

    @Override
    public void deleteSale(String id) {
        // 校验存在
        validateSaleExists(id);
        // 删除
        saleMapper.deleteById(id);
    }

    private void validateSaleExists(String id) {
        if (saleMapper.selectById(id) == null) {
            throw exception(SALE_NOT_EXISTS);
        }
    }

    @Override
    public SaleDO getSale(String id) {
        return saleMapper.selectById(id);
    }

    @Override
    public PageResult<SaleDO> getSalePage(SalePageReqVO pageReqVO) {
        return saleMapper.selectPage(pageReqVO);
    }

    @Override
    public void settleSale(String id) {
        // 校验结款状态是否为等待结款
        SaleDO saleDO = this.getById(id);
        if (saleDO == null) {
            throw exception(SALE_NOT_EXISTS);
        }
        if (saleDO.getPay()) {
            throw exception(SALE_STATUS_INCORRECT);
        }
        // 无“更新时间”字段，其他字段暂时不更新
        this.updateById(
                new SaleDO()
                        .setId(id)
                        .setPay(true)
                        .setUpdateAt(LocalDateTimeUtil.formatNormal(LocalDateTime.now()))
        );
    }
}